package org.agent.action;

import java.util.Date;
import java.util.List;

import org.agent.pojo.Function;
import org.agent.pojo.Premission;
import org.agent.pojo.Role;
import org.agent.service.function.FunctionService;
import org.agent.service.premission.PremissionService;
import org.agent.service.role.RoleService;


public class FunctionAction extends BaseAction {
	private FunctionService functionService;
	private List<Function> functionList;
	private RoleService roleService;
	private List<Role> roleList;
	private Integer roleId;//点击拿到的roleId
	private String checkList;//选择结果
	private PremissionService premissionService;
	
	
	/**
	 * 加载时获取角色列表
	 * */
	public String roleList(){
		this.roleList =this.roleService.getRoleIdAndNameList();
		return SUCCESS;
	}
	/**
	 * 点击角色时获取功能列表
	 * */
	public String funcList(){
		//System.out.println("进入functionList");
		this.functionList = this.functionService.getFunctionList();
		//获取指定rileId角色的权限列表
		Premission premission=new Premission();
		premission.setRoleId(roleId);
		premission.setIsStart(1);
		List<Premission> pList=this.premissionService.getPremissionList(premission);
		//绑定到指定功能---回显数据
		if(pList!=null){
			for (Premission p : pList) {
				for(Function function:functionList){
					if(p.getFunctionId().equals(function.getId())){
						//如果已经有相应功能则true
						function.setChecked(true);; //新创建属性，用来判断
					}
				}
			}
			
		}
		return SUCCESS;
	}

	//保存选择结果的action
	public void saveRoleFunc(){
		//调用业务方法保存角色和功能的映射
		//System.out.println(checkList);
	//拿到的参数roleId，checkList,先删除后添加，添加的属性要加上
		Premission premission =new Premission();
		premission.setRoleId(roleId);
		premission.setCreatedBy(this.getCurrentUser().getUserCode());
		premission.setCreationTime(new Date());
		premission.setIsStart(1);
		premissionService.tx_delAddPremission(premission, this.getCheckList());
		this.getOut().print("success");
		
	}
//set and get
	
	
	
	public FunctionService getFunctionService() {
		return functionService;
	}

	public PremissionService getPremissionService() {
		return premissionService;
	}
	public void setPremissionService(PremissionService premissionService) {
		this.premissionService = premissionService;
	}
	public String getCheckList() {
		return checkList;
	}
	public void setCheckList(String checkList) {
		this.checkList = checkList;
	}
	public Integer getRoleId() {
		return roleId;
	}
	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}
	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}



	public List<Function> getFunctionList() {
		return functionList;
	}



	public void setFunctionList(List<Function> functionList) {
		this.functionList = functionList;
	}



	public RoleService getRoleService() {
		return roleService;
	}



	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}



	public List<Role> getRoleList() {
		return roleList;
	}



	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	
	
	
}
